//
//  MerchantAuthoriVC.swift
//  kxf
//
//  Created by Elavatine on 2024/10/19.
//

import MCToast

class MerchantAuthoriVC: WHBaseViewVC {
    
    var type = "wechat"
    var msgDict = NSDictionary()
    var phone = ""
    
    var authoriBlock:(()->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendRequest()
    }
    lazy var codeVm: MerchantAuthoriItemVM = {
        let vm = MerchantAuthoriItemVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(8), width: 0, height: 0))
        
        if self.type == "wechat"{
            vm.leftTitleLabel.text = "微信子商户号"
            vm.detailLabel.text = self.msgDict["wechatCode"]as? String ?? ""
        }else{
            vm.leftTitleLabel.text = "支付宝子商户号"
            vm.detailLabel.text = self.msgDict["aliCode"]as? String ?? ""
        }
        
        return vm
    }()
    lazy var statusVm: MerchantAuthoriItemVM = {
        let vm = MerchantAuthoriItemVM.init(frame: CGRect.init(x: 0, y: self.codeVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "授权状态"
        vm.detailLabel.textColor = WHColor_16(colorStr: "F10E0E")
        
        return vm
    }()
    lazy var detailVm: MerchantAuthoriItemVM = {
        let vm = MerchantAuthoriItemVM.init(frame: CGRect.init(x: 0, y: self.statusVm.frame.maxY, width: 0, height: 0))
        vm.leftTitleLabel.text = "详情"
        vm.detailLabel.textColor = WHColor_16(colorStr: "F10E0E")
        vm.detailLabel.text = "暂无"
        
        return vm
    }()
    lazy var qrCodeImgView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFit
        return img
    }()
    lazy var refreshBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: SCREEN_HEIGHT-getTabbarHeight()-kFitWidth(48), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("刷新", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(refreshAction), for: .touchUpInside)
        
        return btn
    }()
}


extension MerchantAuthoriVC{
    @objc func refreshAction() {
        sendRequest()
    }
    func sendRequest(){
        MCToast.mc_loading()
        WHNetworkUtil.shareManager().GET(urlString: "\(URL_authori_code)?phone=\(phone)") { responseObject in
            DLLog(message: "\(responseObject)")
            
            if responseObject["code"]as? Int ?? -1 == 200{
                self.msgDict = responseObject["data"]as? NSDictionary ?? [:]
                self.updateUI()
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "")", viewController: self)
            }
        }
    }
}

extension MerchantAuthoriVC{
    func initUI() {
        initNavi(titleStr: "商户信息")
        
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(codeVm)
        view.addSubview(statusVm)
//        view.addSubview(detailVm)
        view.addSubview(qrCodeImgView)
        
        view.addSubview(refreshBtn)
        
        setConstrait()
//        updateUI()
    }
    
    func setConstrait() {
        qrCodeImgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(205))
            make.height.equalTo(kFitWidth(265))
            make.top.equalTo(statusVm.snp.bottom).offset(kFitWidth(88))
        }
    }
    
    func updateUI() {
        if self.type == "wechat"{
            codeVm.detailLabel.text = self.msgDict["weChatCode"]as? String ?? ""
            if self.msgDict["wechatAuthorizeStatus"]as? String ?? "" == "AUTHORIZED" {
                statusVm.detailLabel.text = "已授权"
                if self.authoriBlock != nil{
                    self.authoriBlock!()
                }
            }else{
                statusVm.detailLabel.text = "待授权"
            }
            qrCodeImgView.setImgUrl(urlString: self.msgDict["weChatUrl"]as? String ?? "")
        }else{
            codeVm.detailLabel.text = self.msgDict["aliCode"]as? String ?? ""
            if self.msgDict["alipayAuthorizeStatus"]as? String ?? "" == "AUTHORIZED" {
                statusVm.detailLabel.text = "已授权"
                if self.authoriBlock != nil{
                    self.authoriBlock!()
                }
            }else{
                statusVm.detailLabel.text = "待授权"
            }
            qrCodeImgView.setImgUrl(urlString: self.msgDict["aliUrl"]as? String ?? "")
        }
    }
    /*
     aliCode = 2088940800715488;
     aliUrl = "http://yukuaifu.oss-cn-beijing.aliyuncs.com/auth/13007364724_NO.03/ALIPAYaa1e0d5049324f6c8975fd4eb70c3307.png";
     alipayAuthorizeStatus = UNAUTHORIZED;
     weChatCode = 659347538;
     weChatUrl = "http://yukuaifu.oss-cn-beijing.aliyuncs.com/auth/13007364724_NO.03/WECHAT2f90ea032bb14980aa80074d9e3b7962.png";
     wechatAuthorizeStatus = UNAUTHORIZED;
     */
}
